﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Phone.Framework.Threads;
using System.Threading;
using Microsoft.Phone.Scheduler;

namespace CetWin.Views
{

    

    public partial class Page1 : PhoneApplicationPage
    {
        public Page1()
        {
            InitializeComponent();
            List<Demo> list = new List<Demo>();
            for (int i = 0; i < 10; i++)
            {
                list.Add(new Demo(){Name="name"+i});
            }
            box_Element.ItemsSource = list;
            box_Element.SelectedValuePath = "Name";
       
           // box_Element.SelectedValue = "name2";
        }

        public class Demo
        {
            public string Name { get; set; }
        }

        PeriodicTask periodicTask;
        string periodicTaskName = "CetWinTaskPeriodicAgent";

        private void RemoveAgent(string name)
        {
            try
            {
                ScheduledActionService.Remove(name);
            }
            catch (Exception)
            {
            }
        }


        private void StartPeriodicAgent()
        {
           

            // Obtain a reference to the period task, if one exists
            periodicTask = ScheduledActionService.Find(periodicTaskName) as PeriodicTask;

            // If the task already exists and background agents are enabled for the
            // application, you must remove the task and then add it again to update 
            // the schedule
            if (periodicTask != null)
            {
                RemoveAgent(periodicTaskName);
            }

            periodicTask = new PeriodicTask(periodicTaskName);

            // The description is required for periodic agents. This is the string that the user
            // will see in the background services Settings page on the device.
            periodicTask.Description = "This demonstrates a periodic task.";

            // Place the call to Add in a try block in case the user has disabled agents
            try
            {
                ScheduledActionService.Add(periodicTask);
                ScheduledActionService.LaunchForTest(periodicTaskName, TimeSpan.FromSeconds(10));

//#if DEBUG_AGENT
//            ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(15));
//#endif
                // If debugging is enabled, use LaunchForTest to launch the agent in one minute.

            }
            catch (InvalidOperationException exception)
            {
            }

        }

       
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            StartPeriodicAgent();

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                ScheduledActionService.LaunchForTest(periodicTaskName, TimeSpan.FromSeconds(10));
                // ScheduledActionService.LaunchForTest(periodicTaskName, TimeSpan.FromSeconds(10));

                //#if DEBUG_AGENT
                //            ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(15));
                //#endif
                // If debugging is enabled, use LaunchForTest to launch the agent in one minute.

            }
            catch (InvalidOperationException exception)
            {
                MessageBox.Show(exception.Message);
            }
           
          //  RemoveAgent(periodicTaskName);
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            RemoveAgent(periodicTaskName);
        }
    }
}